package com.ship.contract.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ship.common.core.domain.R;
import com.ship.common.core.exception.BusException;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.contract.bean.*;
import com.ship.contract.mapper.*;
import com.ship.contract.service.ConFilesService;
import com.ship.contract.utils.FileUtil;
import com.ship.system.api.RemoteFileService;
import com.ship.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author tangshixuan
 * @date 2023-9-1
 */
@Service
public class ConFilesServiceImpl extends ServiceImpl<ConFilesMapper, ConFiles> implements ConFilesService {
    @Autowired
    private ConFilesMapper conFilesMapper;
    @Autowired
    private ConResourceMainMapper conResourceMainMapper;
    @Autowired
    private ConResourceMemorandumMapper conResourceMemorandumMapper;
    @Autowired
    private ConShippingMapper conShippingMapper;
    @Autowired
    private ConShippingAgencyMapper conShippingAgencyMapper;
    @Autowired
    private ConBrokerMapper conBrokerMapper;
    @Autowired
    private ConThirdPartyMeasurementMapper conThirdPartyMeasurementMapper;
    @Autowired
    private ConInsuranceGoodsMapper conInsuranceGoodsMapper;
    @Autowired
    private ConInsurancePeopleMapper conInsurancePeopleMapper;
    @Autowired
    private ConRefuelingMapper conRefuelingMapper;
    @Autowired
    private ConLawyerMapper conLawyerMapper;
    @Autowired
    private ConTemplateMapper conTemplateMapper;

    @Autowired
    private RemoteFileService remoteFileService;

    @Override
    public List<ConFiles> listByConId(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("con_id",id);
        return super.list(queryWrapper);
    }

    @Override
    public ConFiles getContractMainFile(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("con_id",id);
        queryWrapper.eq("file_type",1);
        return super.getOne(queryWrapper,false);
    }

    @Override
    public void saveFile(ConFiles conFiles) {
        Long conId = conFiles.getConId();
        List<ConFiles> existingConFiles = conFilesMapper.getConFilesByConIdAndFileType(conId, 1);
        if (!existingConFiles.isEmpty()) {
            // 如果存在相同conId并且fileType为1的记录，执行更新操作
            ConFiles existingConFile = existingConFiles.get(0);
            existingConFile.setFileType(1);
            Boolean isScanPdf = FileUtil.getIsScanPdf(conFiles.getUrl());
            existingConFile.setScanPdf(isScanPdf);
//            if (isScanPdf){
//                //绑定非扫描件pdf的url
//                MultipartFile notScanPdf = FileUtil.getNotScanPdf(conFiles.getUrl());
//                R<SysFile> result = remoteFileService.upload(notScanPdf);
//                if (result.getCode() != R.SUCCESS) {
//                    throw new BusException("文件服务器连接失败", BaseResultCode.GENERAL_ERROR);
//                }
//                String httpUrl = result.getData().getHttpUrl();
//                existingConFile.setUrl(httpUrl);
//                existingConFile.setNewUrl(conFiles.getUrl());
//            }else {
//                existingConFile.setUrl(conFiles.getUrl());
//            }
            existingConFile.setUrl(conFiles.getUrl());
            String path = conFiles.getUrl();
            existingConFile.setFileName(path.substring(path.lastIndexOf("/") + 1, path.length()));
            super.updateById(existingConFile);
        } else {
            // 如果不存在相同conId并且fileType为1的记录，执行插入操作
            ConFiles newConFile = new ConFiles();
            newConFile.setConId(conFiles.getConId());
            newConFile.setUrl(conFiles.getUrl());
            Boolean isScanPdf = FileUtil.getIsScanPdf(conFiles.getUrl());
            newConFile.setScanPdf(isScanPdf);
//            if (isScanPdf){
//                //绑定非扫描件pdf的url
//                MultipartFile notScanPdf = FileUtil.getNotScanPdf(conFiles.getUrl());
//                R<SysFile> result = remoteFileService.upload(notScanPdf);
//                if (result.getCode() != R.SUCCESS) {
//                    throw new BusException("文件服务器连接失败", BaseResultCode.GENERAL_ERROR);
//                }
//                String httpUrl = result.getData().getHttpUrl();
//                newConFile.setUrl(httpUrl);
//                newConFile.setNewUrl(conFiles.getUrl());
//            }else {
//                newConFile.setUrl(conFiles.getUrl());
//            }
            newConFile.setUrl(conFiles.getUrl());
            newConFile.setFileType(1);
            String path = conFiles.getUrl();
            newConFile.setFileName(path.substring(path.lastIndexOf("/") + 1, path.length()));
            super.save(newConFile);
        }
    }

    @Override
    public void updateConStatus(Long conId){
        ConResourceMain conResourceMain = conResourceMainMapper.selectById(conId);
        ConResourceMemorandum conResourceMemorandum = conResourceMemorandumMapper.selectById(conId);
        ConShipping conShipping = conShippingMapper.selectById(conId);
        ConShippingAgency conShippingAgency = conShippingAgencyMapper.selectById(conId);
        ConBroker conBroker = conBrokerMapper.selectById(conId);
        ConThirdPartyMeasurement conThirdPartyMeasurement = conThirdPartyMeasurementMapper.selectById(conId);
        ConInsuranceGoods conInsuranceGoods = conInsuranceGoodsMapper.selectById(conId);
        ConInsurancePeople conInsurancePeople = conInsurancePeopleMapper.selectById(conId);
        ConRefueling conRefueling = conRefuelingMapper.selectById(conId);
        ConLawyer conLawyer = conLawyerMapper.selectById(conId);
        ConTemplate conTemplate = conTemplateMapper.selectById(conId);
        if(null != conResourceMain){
            conResourceMain.setStatus(2);
            conResourceMainMapper.updateById(conResourceMain);
        }
        if(null != conResourceMemorandum){
            conResourceMemorandum.setStatus(2);
            conResourceMemorandumMapper.updateById(conResourceMemorandum);
        }
        if(null != conShipping){
            conShipping.setStatus(2);
            conShippingMapper.updateById(conShipping);
        }
        if(null != conShippingAgency){
            conShippingAgency.setStatus(2);
            conShippingAgencyMapper.updateById(conShippingAgency);
        }
        if(null != conBroker){
            conBroker.setStatus(2);
            conBrokerMapper.updateById(conBroker);
        }
        if(null != conThirdPartyMeasurement){
            conThirdPartyMeasurement.setStatus(2);
            conThirdPartyMeasurementMapper.updateById(conThirdPartyMeasurement);
        }
        if(null != conInsuranceGoods){
            conInsuranceGoods.setStatus(2);
            conInsuranceGoodsMapper.updateById(conInsuranceGoods);
        }
        if(null != conInsurancePeople){
            conInsurancePeople.setStatus(2);
            conInsurancePeopleMapper.updateById(conInsurancePeople);
        }
        if(null != conRefueling){
            conRefueling.setStatus(2);
            conRefuelingMapper.updateById(conRefueling);
        }
        if(null != conLawyer){
            conLawyer.setStatus(2);
            conLawyerMapper.updateById(conLawyer);
        }
        if(null != conTemplate){
            conTemplate.setStatus(2);
            conTemplateMapper.updateById(conTemplate);
        }
    }
}
